#include <windows.h>
#include <extend.h>

static LONG lMemUsed = 0;

//----------------------------------------------------------------------------//

void _bcopy( LPVOID pDest, LPVOID pOrg, WORD wLen )
{
   WORD w = 0;

   while( w++ < wLen )
      * ( ( LPBYTE ) pDest )++ = * ( ( LPBYTE ) pOrg )++;
}

//----------------------------------------------------------------------------//

void _bset( LPVOID pDest, BYTE bValue, WORD wLen )
{
   WORD w = 0;

   while( w++ < wLen )
      * ( ( LPBYTE ) pDest )++ = bValue;
}

//----------------------------------------------------------------------------//

POVM _vmAlloc( LONG lSize )
{
   POVM pOVM = ( POVM ) GlobalAlloc( LHND, lSize );
//   BYTE bTemp[ 10 ];

   lMemUsed += lSize;

//   _str( lSize, bTemp );
//   LogFile( "mem.log", bTemp );

   if( ! pOVM )
      MsgAlert( "cant allocate memory", "From _vmAlloc()" );
   else
      return pOVM;
}

//----------------------------------------------------------------------------//

LPVOID _vmLock( POVM pOVM )
{
   LPVOID pMem = GlobalLock( ( HGLOBAL ) pOVM );

   if( ! pMem )
      MsgAlert( "cant lock memory", "From _vmLock()" );
   else
      return pMem;
}

//----------------------------------------------------------------------------//

void _vmUnLock( POVM pOVM )
{
   GlobalUnlock( ( HGLOBAL ) pOVM );
}

//----------------------------------------------------------------------------//

LPVOID _vmRealloc( POVM pOVM, LONG lSize )
{
   lMemUsed += lSize - GlobalSize( pOVM );

   return GlobalReAlloc( ( HGLOBAL ) pOVM, lSize, LHND );
}

//----------------------------------------------------------------------------//

LONG _vmSize( POVM pOVM )
{
   return GlobalSize( ( HGLOBAL ) pOVM );
}

//----------------------------------------------------------------------------//

void _vmFree( POVM pOVM )
{
   lMemUsed -= GlobalSize( pOVM );

   GlobalFree( ( HGLOBAL ) pOVM );
}

//----------------------------------------------------------------------------//

LPVOID _xgrab( LONG lSize )
{
   return _vmLock( _vmAlloc( lSize ) );
}

//----------------------------------------------------------------------------//

CLIPPER MEMUSED()
{
   _retnl( lMemUsed );
}

//----------------------------------------------------------------------------//
